package com.example.zytphone;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.zytphone.adapter.RecycleViewAdapter;
import com.example.zytphone.base.BPData;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<BPData> list;
    private TextView real_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        addBPData();
        initView();
        Startthread();

    }
    private void Startthread(){
        new Thread(){
            @Override
            public void run() {
                do {
                    try {
                        Thread.sleep(1000);
                        Message message=new Message();
                        message.what=1;
                        handler.sendMessage(message);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }while (true);
            }
        }.start();
     }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    long time = System.currentTimeMillis();
                    CharSequence format = DateFormat.format("hh:mm:ss yyyy-MM-dd", time);
                    real_time.setText(format);
                    break;
            }
        }
    };

    //判断是否是主线程
    private boolean isMain(){
        Thread thread = Looper.getMainLooper().getThread();
        boolean b = thread == Thread.currentThread();
        return b;
    }

    //添加数据
    private void addBPData() {
       list=new ArrayList<>();
       list.add(new BPData(22,56,"format"));
       list.add(new BPData(45,78,"format"));
       list.add(new BPData(32,42,"format"));
       list.add(new BPData(221,36,"format"));
       list.add(new BPData(65,74,"format"));
    }

    private void initView() {
        real_time=findViewById(R.id.real_time);
        recyclerView=findViewById(R.id.recycle);
        //拿到布局加载器  线性布局
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecycleViewAdapter adapter=new RecycleViewAdapter(list);
        recyclerView.setAdapter(adapter);
    }

}